package com.timerecording.controller.newSystem;

import com.aliyun.oss.OSS;
import com.timerecording.common.SwaggerTagConst;
import com.timerecording.common.controller.NewRecordTimeAdminBaseController;
import com.timerecording.domain.entity.newSystem.NewRecordTimeEntity;
import com.timerecording.domain.form.NewRecordTimeAddForm;
import com.timerecording.domain.form.RecordTimeAddForm;
import com.timerecording.domain.vo.newSystem.NewRecordTimeVO;
import com.timerecording.service.newSystem.NewRecordTimeService;
import com.timerecording.util.AliyunOssUtil;
import com.timerecording.util.R;
import com.timerecording.util.ResponseDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author smz41
 * @description: TODO
 * @date 2024/8/4
 */
@CrossOrigin  //解决跨域问题
@RestController
@Api(tags = SwaggerTagConst.Admin.NEW_RECORDING_TIME_SYSTEM)   //api前缀
public class NewRecordTimeController extends NewRecordTimeAdminBaseController {

    @Resource
    private NewRecordTimeService newRecordTimeService;


    /**
     * 工时记录列表查询,可根据时间进行范围查询
     *
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/8/4
     */

    @ApiOperation("工时记录列表查询 @author smz")
    @GetMapping("/queryNewRecordTime")
    public ResponseDTO<List<NewRecordTimeVO>> queryNewRecordTime(@ApiParam("查询本月开始时间") String recordStartDate,
                                                                 @ApiParam("查询本月结束时间") String recordEndDate,
                                                                 @ApiParam("查询当天数据的时间") String toDayTime) {
        return newRecordTimeService.queryNewRecordTime(recordStartDate, recordEndDate, toDayTime);

    }


    /**
     * 新增工时记录
     *
     * @description: TODO
     * @author sqt
     * @version 1.0
     * @date 2024/8/4
     */

    @ApiOperation("新增工时记录 @author sqt")
    @PostMapping("addNewRecordTime")
    public R insertNewRecordTime(@ApiParam("添加工时记录") @RequestBody @Valid NewRecordTimeAddForm recordAddForm) throws UnknownHostException {
        return newRecordTimeService.insertNewRecordTime(recordAddForm);
    }

    /**
     * 新增工时记录
     *
     * @description: TODO
     * @author sqt
     * @version 1.0
     * @date 2024/8/4
     */
    @ApiOperation("查询工时详情 @author sqt")
    @GetMapping("/newInfoRecordTime/{recordId}")
    public ResponseDTO<NewRecordTimeEntity> newInfoRecordTime(@ApiParam(value = "工时表主键id") @PathVariable Integer recordId) throws UnknownHostException {
        return newRecordTimeService.newInfoRecordTime(recordId);
    }


    /**
     * 逻辑删除工时记录
     *
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/9/8
     */

    @ApiOperation("逻辑删除工时记录 @author smz")
    @PostMapping("/deleteNewRecordTime/{recordId}")
    public R deleteNewRecordTime(@ApiParam("工时记录主表id") @PathVariable Integer recordId) throws UnknownHostException {
        newRecordTimeService.deleteNewRecordTime(recordId);
        return R.ok();
    }


    /**
     * 恢复逻辑删除工时记录
     *
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/9/8
     */
    @ApiOperation("恢复逻辑删除工时记录 @author smz")
    @PostMapping("/recoverSalaryById/{recordId}")
    public R recoverSalaryById(@ApiParam("工时记录主表id") @PathVariable Integer recordId) throws UnknownHostException {
        newRecordTimeService.recoverSalaryById(recordId);
        return R.ok();
    }


//    @ApiOperation("修改密码 @author smz")
//    @PostMapping("")


    /** OSS上传图片
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2025/7/2
     */

    public Map<String,String> uploadFile(MultipartFile multipartFile){
        //获取是否获取到文件
        System.out.println("multipartFile = " + multipartFile.getOriginalFilename());
        OSS ossClint = AliyunOssUtil.getOSSClint();
        Map<String,String> resultMap = new HashMap<>();
        try {
            //存入到oss-smz文件夹下面的img文件夹
            ossClint.putObject("oss-smz","img/"+multipartFile.getOriginalFilename(),multipartFile.getInputStream());
            resultMap.put("url","img/"+multipartFile.getOriginalFilename());
        } catch (IOException e) {
            resultMap.put("message","上传失败");
            e.printStackTrace();
        }
        return resultMap;
    }


}
